﻿Imports System.Data.OleDb

Public Class frmCategoria

    Dim connexion As OleDbConnection
    Dim command As OleDbCommand

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bd.getCategoria("", "frmCategoria", "grid")

        CategoriaDataGridView.RowHeadersVisible = False
        CategoriaDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        CategoriaDataGridView.ClearSelection()

    End Sub

    Private Sub btnGuargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuargar.Click

        If btnGuargar.Text = "Nuevo" Then
            limpia()
            btnGuargar.Text = "Guardar"
        Else
            connexion = New OleDbConnection(bd.sConexion)
            command = New OleDbCommand("INSERT INTO categoriaProducto (Descripcion) values (DESC) ", connexion)

            command.Parameters.Add(New OleDbParameter("@DESC", OleDbType.VarWChar, 20))
            command.Parameters("@DESC").Value = DescripcionTextBox.Text

            'mando sql a base de datos
            command.Connection.Open()
            Dim result As Integer = command.ExecuteNonQuery()
            command.Connection.Close()

            If result > 0 Then
                bd.getCategoria("", "frmCategoria", "grid")
                limpia()
                MsgBox("Registro Insertado")
            End If

        End If

    End Sub

    Private Sub CategoriaDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CategoriaDataGridView.CellClick
        IdCategoriaTextBox.Text = CategoriaDataGridView.Item(0, CategoriaDataGridView.CurrentRow.Index).Value
        DescripcionTextBox.Text = CategoriaDataGridView.Item(1, CategoriaDataGridView.CurrentRow.Index).Value

        btnGuargar.Text = "Nuevo"
        btnActualizar.Enabled = True
        btnElminar.Enabled = True
    End Sub

    Private Sub btnElminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnElminar.Click


        connexion = New OleDbConnection(bd.sConexion)
        command = New OleDbCommand("DELETE FROM categoriaProducto WHERE IdCategoria = @ID ", connexion)

        command.Parameters.Add(New OleDbParameter("@ID", OleDbType.VarWChar, 20))
        command.Parameters("@ID").Value = IdCategoriaTextBox.Text

        'mando sql a base de datos
        command.Connection.Open()
        Dim result As Integer = command.ExecuteNonQuery()
        command.Connection.Close()

        If result > 0 Then
            bd.getCategoria("", "frmCategoria", "grid")
            limpia()
            MsgBox("Registro Eliminado")
        End If

    End Sub

    Sub limpia()
        IdCategoriaTextBox.Clear()
        DescripcionTextBox.Clear()
        btnGuargar.Text = "Guardar"
        CategoriaDataGridView.ClearSelection()
        btnActualizar.Enabled = False
        btnElminar.Enabled = False
    End Sub

    Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click
        connexion = New OleDbConnection(bd.sConexion)
        command = New OleDbCommand("UPDATE categoriaProducto SET Descripcion = '" & DescripcionTextBox.Text & "' WHERE IdCategoria = @ID", connexion)

        command.Parameters.Add(New OleDbParameter("@ID", OleDbType.Integer, 20))
        command.Parameters("@ID").Value = IdCategoriaTextBox.Text

        'mando sql a base de datos
        command.Connection.Open()
        Dim result As Integer = command.ExecuteNonQuery()
        command.Connection.Close()

        If result > 0 Then
            bd.getCategoria("", "frmCategoria", "grid")
            limpia()
            MsgBox("Registro Actualizado")
        End If
    End Sub


End Class
